﻿using Rabbit.Modal;
using System.Collections.Generic;
using System.Linq;
namespace Rabbit.BusinessLogicLayer
{
    public class CommentsBL : AutoUpdateMessageClassBase<Comment>
    {
        private List<Comment> commentList = new List<Comment>();

        public CommentsBL(double UpdateInterval)
            : base(UpdateInterval)
        {

        }

        #region GetMyComments
        public static Comments GetMyComments(double? sinceID)
        {
            string parameters = "cursor=-1";
            if (sinceID == null)
            {
                parameters += "&count=" + Utils.Configuration.PageSize;
            }
            else
            {
                parameters += "&since_id=" + sinceID.Value;
            }
            string result = RequestHelper.SendRequest("statuses/comments_timeline.json", parameters);
            Comments tmpStatus = WrappedJsonHelper.Deserialize<Comments>(result);
            return tmpStatus;
        }
        #endregion

        protected override void FetchNewMessage()
        {
            List<Comment> commentList = GetMyComments(LastMaxID).CommentList;
            if (commentList.Count > 0)
            {
                LastMaxID = commentList.Max(c => c.Id);
                commentList.ForEach(c => c.IsNew = true);
            }
            RaiseNewMessageReceivedEvent(commentList);
        }

        protected override void FetchMessage()
        {
            List<Comment> commentList = GetMyComments(null).CommentList;
            LastMaxID = commentList.Max(c => c.Id);

            RaiseMessageFetchedEvent(commentList);
        }
    }
}
